-- absorb
-- created by panyl
-- 吸血N%

return {
    apply = function(prop, baseValue)
        local v = math.modf(baseValue * prop[3] / 1000);
        return baseValue + v;
    end,

    -- 对敌伤害触发
    trigger = function(who, prop, arg)
        local hp = math.modf(arg * prop[3] / 1000);
        if hp <= 0 then
            hp = 1;
        end

        if who.type == OBJECT_TYPE_USER then
            BonusM.doBonus({ 2, "hp", hp, }, "prop absorb");
            EventMgr.fire(event.HP_VAMPIRE, hp);

            -- 恢复魔法，恢复固定值
            local p = PropM.combine(who, "magic_revive", 53);
            local mp = PropM.apply(p, 1000);
            if mp > 0 then
                BonusM.doBonus({ 2, "mp", mp, }, "prop absorb");
                EventMgr.fire(event.MP_VAMPIRE, mp);
            end
        else
            if prop[2] == 2 then
                if true ~= DungeonM.canRecoverAttrib(ME.user, "hp") then
                    -- 玩家此时的状态无法恢复hp
                    return 0;
                end

                -- 将吸收的血量转移给玩家
                BonusM.doBonus({ 2, "hp", hp, }, "prop absorb");
                if ME.user:queryAttrib("max_hp") > ME.user:queryAttrib("hp") then
                    EventMgr.fire(event.HP_VAMPIRE, hp);
                end
                return hp;
            end

            who:addAttrib("hp", hp);
        end

        return hp;
    end,

    desc = function(prop)
        local row = PropM.getRow(prop[1]);
        local desc = row.desc;

        local key = FormulaM.invoke("CALC_SKILL_APPEND_PROP", 53);
        if key then
            local p = PropM.combine(ME.user, key, 53);
            local append = math.modf(PropM.apply(p, prop[3]) / 10);
            if append > 0 then
                desc = row.append_desc;
                desc = string.gsub(desc, "{append}", append);
            end
        end

        local v = math.modf(prop[3] / 10);
        desc = string.gsub(desc, "{1}", v);

        return desc;
    end,

    record = function(target, prop, path)
        return false;
    end
};